<?php
	
	namespace Aspic;

	class DataFilter {
		const FILTER_METHODS_PREFIX = 'filter_';
		
		/**
		* @param string $src The string to filter
		* @param array $filters An array containing the filters to apply (in the given order)
		* Each line contains whether only the filter name, whether an array with as first row, 
		* the filter name and next rows contains filter parameters
		* 
		* @param bool $filtersParamsInOneArray Allow dynamic call of the method, if true when params are set, 
		* the params must be in an array as the second line of the filter array. If set to false, each line after the first
		* in the filters array, owns a different param.
		*/
		public static function filter($src, $filters, $filtersParamsInOneArray = false) {
			$out = $src;
			
			foreach($filters as $filter) {
				$args = array($out); // This is the arguments that will be passed to the filter function => so first the src and then the params
				
				if(is_array($filter)) {
					$filterName = $filter[0];
					$args = array_merge($args, array_slice($filter, 1));
				}
				else {
					$filterName = $filter;
				}
				
				$meth = static::FILTER_METHODS_PREFIX.$filterName;
				
				if(method_exists(get_class(), $meth)) {
					if($filtersParamsInOneArray) {
						$args = array_merge(
							array($args[0]),  // The source
							$args[1] // The params
						);
						
						$out = call_user_func_array(array(get_class(), $meth), $args);
					}
					else {
						$out = call_user_func_array(array(get_class(), $meth), $args);
					}
				}
			}
			
			return $out;
		}
		
		/**
		* Used like: DataFilter::truncate('value');
		*/
		public static function __callStatic($name, $args) {
			$methodName = static::FILTER_METHODS_PREFIX.$name;
			
			if(method_exists(get_called_class(), $methodName)) {
				return call_user_func_array(array(get_called_class(), $methodName), $args);
			}
			else {
				throw new Exception('Unknown method "'.$name.'"');
			}
		}
		
		protected static function filter_int($src) {
			settype($src, 'integer');
			
			return $src;
		}
		
		protected static function filter_decimalSeparator($src, $separator = null) {
			if(is_null($separator)) {
				$separator = '.';
			}
			
			$src = str_replace(array('.', ','), $separator, $src);
			
			return $src;
		}
		
		protected static function filter_truncate($src, $truncValue) {
			$out = substr($src, 0, $truncValue);
			
			return $out;
		}
		
		protected static function filter_specialLettersChars($src) {
			$out = UString::replaceSpecialLettersChars($src);
			
			return $out;
		}
		
		protected static function filter_alpha($src) {
			$out = preg_replace('@[^a-zA-Z0-9_]@', '', $src);
			
			return $out;
		}
		
		protected static function filter_trim($src) {
			$out = trim($src);
			
			return $out;
		}
		
		protected static function filter_rtrim($src) {
			$out = rtrim($src);
			
			return $out;
		}
		
		protected static function filter_ltrim($src) {
			$out = ltrim($src);
			
			return $out;
		}
		
		// protected static function filter_($src) {
			// $out = 
			
			// return $out;
		// }
		
	}
	
?>